/**
 * Copyright (C)  Gadglet .
 *
 * This file is part of Gadglet
 *
 * Gadglet is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gadglet is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Gadglet. If not, see <http://www.gnu.org/licenses/>.
 */

package com.gadglet.gadgets.phoneBook.client;


import com.gadglet.client.gwt.ui.DebugDialogBox;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.gadgets.client.LanguageDirection;
import com.google.gwt.gadgets.client.PreferencesFeature;
import com.google.gwt.gadgets.client.PreferencesProvider;
import com.google.gwt.gadgets.client.impl.PreferencesUtil;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.TabBar;
import com.google.gwt.user.client.ui.VerticalPanel;

public class PhoneBookPanel {
	
	    VerticalPanel phoneBookPanel = new VerticalPanel();
	    VerticalPanel tabsPanel = new VerticalPanel();
	    static public VerticalPanel data = new VerticalPanel();
	    static public String [] tabLabels;
	    int itemsOnTabBar = 16;
	    private DebugDialogBox errorNotifier = DebugDialogBox.getErrorNotifier();
	    
	    PhoneBookPanel(){}
	     VerticalPanel buildUI() 
	    {
	    	
	 		PreferencesFeature  prefUtil = PreferencesProvider.get();
			
			if(LanguageDirection.getDir()!=null && LanguageDirection.getDir().getValue()!=null && LanguageDirection.getDir().getValue().equalsIgnoreCase("rtl"))
				data.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
			try
			{
				tabLabels = prefUtil.getMsg("gadgetLabelAlphabet").split(",");
			
			}
			catch (Exception e)
			{
				
					errorNotifier.showError(1, e.getMessage());
			}
			
	        
	  	       
	        int tabsNum = tabLabels.length/itemsOnTabBar + (tabLabels.length % itemsOnTabBar > 0 ? 1 : 0);
	       	       
	       
	        SelectionHandler <Integer> handler = new SelectionHandler<Integer>(){
	            @Override
				public void onSelection(SelectionEvent<Integer> event) {
	                  int activeNavBarNum = clearPrevSelect((TabBar)event.getSource());
	                  data.clear();
	                  ResultsGrid rg = new ResultsGrid(activeNavBarNum*itemsOnTabBar+event.getSelectedItem().intValue());
	                  rg.changeData(0);
	                 
	            }};
	           
	        int tabsWidht = 0; 
	        for(int row=0;row<tabsNum;row++){
	               
	                TabBar tab = new TabBar();
	                
	                tabsPanel.add(tab);
	                tab.addSelectionHandler(handler);
	               
	                for(int item=0;item<itemsOnTabBar;item++){
	                    if((row*itemsOnTabBar+item)<tabLabels.length)
	                         tab.addTab(tabLabels[row*itemsOnTabBar+item]);
	                }
	                if(tabsWidht<tab.getOffsetWidth())
	                    tabsWidht = tab.getOffsetWidth();
	        }
	       
	        data.setWidth("100%");
	        data.addStyleName("gwt-TabPanelBottom");
	        phoneBookPanel.add(tabsPanel);
	        phoneBookPanel.add(data);
	        
	        
	    
	        return phoneBookPanel;
	       
	    }
	   
	   
	   
	     int  clearPrevSelect(TabBar activeTabBar)
	    {
	        int activeNavBarNum = -1 ;
	        for(int wig = 0; wig<tabsPanel.getWidgetCount();wig++){
	           
	            try {
	                           
	                TabBar tb = (TabBar) tabsPanel.getWidget(wig);
	                           
	                if(!activeTabBar.equals(tb))
	                    tb.selectTab(-1);
	                else
	                    activeNavBarNum = wig;
	                   
	            }
	            catch (Exception e) {
	            	if (PhoneBook.debug)
						errorNotifier.showError(1, e.getMessage());
	            }
	        }
	       
	        return activeNavBarNum;
	    }
}
